﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Netcaso_MVC.WebUtilities;
using TimKiemViet_VN.Models;
using TimKiemViet_VN.DataService;

namespace TimKiemViet_VN.Controllers
{
    public class AdminHTMLControlController : Controller
    {
        public ActionResult HTMLControlManagement()
        {
            if (!User.IsInRole("Administrator"))
            {
                return RedirectToAction("Login", "Account");
            }
            ViewBag.Title = MVCLocalization.getLocale("HTMLControlManagement"); 

            // get data
            HTMLControlModel model = new HTMLControlModel();
            ViewBag.ListHTMLContent = model.NCS_HTMLControls;

            string id = "";
            if (RouteData.Values["id"] != null)
            {
                id = RouteData.Values["id"].ToString();
            }
            else
            {
                // get the first data
                id = model.NCS_HTMLControls[0].ModuleID.ToString();
                ViewBag.hdID = id;
            }
            model = new HTMLControlModel(int.Parse(id));
            ViewBag.model = model;

            return View();
        }

        [HttpPost]
        public ActionResult HTMLControlManagement(string id)
        {
            if (!User.IsInRole("Administrator"))
            {
                return RedirectToAction("Login", "Account");
            }
            ViewBag.Title = MVCLocalization.getLocale("HTMLControlManagement");

            string strName = Request.Params["txtName"].ToString();
            string strContent = Request.Params["txtContent"].ToString();
            string hdID = Request.Params["hdID"] != null ? Request.Params["hdID"].ToString() : "";
            HTMLControlModel model = new HTMLControlModel();

            if (id == null || id == "")
            {
                id = hdID;
            }

            model.updateHTMLControlByModuleID(new NCS_HTMLControl
            {
                ModuleID = int.Parse(id),
                Name = strName,
                Content = strContent,
                Locale = MVCLocalization.LocaleKey
            });

            // get data
            ViewBag.ListHTMLContent = model.NCS_HTMLControls;

            if (id != string.Empty)
            {
                model = new HTMLControlModel(int.Parse(id));
                ViewBag.model = model;
            }

            return View();
        }
    }
}
